package com.abc.recycling.controller;

import com.abc.recycling.domain.statistics.ChartData;
import com.abc.recycling.domain.statistics.RegionStatistics;
import com.abc.recycling.domain.statistics.StatisticsOverview;
import com.abc.recycling.model.statistics.RankingData;
import com.abc.recycling.service.StatisticsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/statistics")
@RequiredArgsConstructor
public class UserStatisticsController {

    private final StatisticsService statisticsService;

    @GetMapping("/overview")
    public String overview(
            @RequestParam(defaultValue = "month") String range,
            Model model
    ) {
        // 获取统计概览数据
        StatisticsOverview stats = statisticsService.getOverview(range);
        model.addAttribute("stats", stats);
        
        // 获取趋势图数据
        ChartData trendData = statisticsService.getTrendData(range);
        model.addAttribute("trendData", trendData);
        
        // 获取物品类型分布数据
        ChartData typeData = statisticsService.getTypeDistribution(range);
        model.addAttribute("typeData", typeData);
        
        // 获取排行榜数据（默认显示前10名）
        model.addAttribute("stationRanking", statisticsService.getStationRanking(range, 10));
        model.addAttribute("collectorRanking", statisticsService.getCollectorRanking(range, 10));
        
        return "statistics/overview";
    }

    @GetMapping("/region")
    public String regionStatistics(
            @RequestParam(defaultValue = "month") String range,
            Model model
    ) {
        // 获取所有区域的统计数据
        List<RegionStatistics> regionStats = statisticsService.getRegionStatistics(range);
        model.addAttribute("regionStats", regionStats);
        
        return "statistics/region";
    }

    @GetMapping("/region/{region}")
    public String regionDetail(
            @PathVariable String region,
            @RequestParam(defaultValue = "month") String range,
            Model model
    ) {
        // 获取区域详细统计数据
        RegionStatistics stats = statisticsService.getRegionDetail(region, range);
        model.addAttribute("stats", stats);
        
        // 获取区域趋势数据
        ChartData trendData = statisticsService.getRegionTrend(region, range);
        model.addAttribute("trendData", trendData);
        
        return "statistics/region-detail";
    }
} 